package com.YCH.behavior.service.impl;

import com.YCH.behavior.service.AppShowBehaviorService;
import com.YCH.model.behavior.dtos.ShowBehaviorDto;
import com.YCH.model.behavior.dtos.ViewArticleDto;
import com.YCH.model.behavior.pojos.TbShowBehavior;
import com.YCH.model.common.dtos.ResponseResult;
import com.YCH.model.common.enums.AppHttpCodeEnum;
import com.YCH.model.mappers.app.TbShowBehaviorMapper;
import com.YCH.model.user.pojos.TbUser;
import com.YCH.utils.threadlocal.AppThreadLocalUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;

@Service
@SuppressWarnings("all")
public class AppShowBehaviorServiceImpl implements AppShowBehaviorService {
    @Autowired
    private TbShowBehaviorMapper tbShowBehaviorMapper;

    @Override
    public ResponseResult saveShowBehavior(ShowBehaviorDto dto) {
        //获取用户信息，获取设备id
        // 1.校验参数
        TbUser user = AppThreadLocalUtils.getUser();
        // 用户和用户id不能同时为空
        if (user == null && dto.getUserId() == null) {
            return ResponseResult.errorResult(AppHttpCodeEnum.PARAM_REQUIRE);
        }
        Long userId = null;
        if (user != null) {
            userId = user.getId();
        }
        if(dto.getUserId()!=null){
            userId = dto.getUserId();
        }
        //获取前台传递过来的文章列表id
        // List<ApArticle> articleIds = dto.getArticleIds();
        Long[] articleIds = new Long[dto.getArticleIds().size()];
        for (int i = 0; i < articleIds.length; i++) {
            articleIds[i] = dto.getArticleIds().get(i).getId();
        }

        //根据行为实体id和文章列表id查询app行为表  ap_show_behavior
        List<TbShowBehavior> apShowBehaviors = tbShowBehaviorMapper.selectListByUserIdAndArticleIds(userId, articleIds);
        //数据的过滤，需要删除表中已经存在的文章id
        List<Long> integers = new ArrayList<>(Arrays.asList(articleIds));
        if (!apShowBehaviors.isEmpty()) {
            apShowBehaviors.forEach(item -> {
                Long articleId = item.getArticleId();
                for(int i = 0; i < integers.size(); i++){
                    if (integers.get(i) == articleId) {
                        integers.remove(integers.indexOf(articleId));
                    }
                }
            });
        }
        //保存操作
        if (!integers.isEmpty()) {
            articleIds = new Long[integers.size()];
            integers.toArray(articleIds);
            tbShowBehaviorMapper.saveShowBehavior(articleIds, userId);
        }
        return ResponseResult.okResult(AppHttpCodeEnum.SUCCESS);
    }

    // 是否点击查看文章
    @Override
    public ResponseResult saveViewArticleBehavior(ViewArticleDto dto) {
        //获取用户信息，获取设备id
        // 1.校验参数
        TbUser user = AppThreadLocalUtils.getUser();
        // 用户和用户id不能同时为空
        if (user == null && dto.getUserId() == null) {
            return ResponseResult.errorResult(AppHttpCodeEnum.PARAM_REQUIRE);
        }
        Long userId = null;
        if (user != null) {
            userId = user.getId();
        }
        if(dto.getUserId()!=null){
            userId = dto.getUserId();
        }
        //获取前台传递过来的文章列表id
        // List<TbArticle> articleIds = dto.getArticleIds();
        // 没有返回参数错误
        if (dto.getArticleId() == null) {
            return ResponseResult.errorResult(AppHttpCodeEnum.PARAM_REQUIRE);
        }
        // 查询该article 是否存在；
        TbShowBehavior tbShowBehavior = tbShowBehaviorMapper.selectShowBehaviorByUserIdAndArticleId(userId, dto.getArticleId());
        // //数据的过滤，需要删除表中已经存在的文章id
        // 数据不存在
        if (tbShowBehavior == null) {
            return ResponseResult.errorResult(AppHttpCodeEnum.DATA_NOT_EXIST);
        }
        // 当isClick为true，即该用户已经查看
        if (tbShowBehavior.getIsClick()) {
            return ResponseResult.okResult(0, "本用户已经查看");
        }
        // //更新操作
        tbShowBehaviorMapper.updateShowBehavior(userId, dto.getArticleId(), dto.getIsClick());
        return ResponseResult.okResult(AppHttpCodeEnum.SUCCESS);
    }
}
